﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZNYBSProject
{
    class SyncLog
    {
        public SyncLog(string STCD)
        {
            LogDirectory = Application.StartupPath + "\\" + STCD;
        }


        /// <summary>
        /// 本地日志文件存放路径
        /// </summary>
        private string LogDirectory = Application.StartupPath + "\\Log";

        #region 日志写入本地文件
        private string GetLogFilePath(string TableName)
        {
            if (!System.IO.Directory.Exists(LogDirectory))//检测文件夹是否存在，再创建
            {
                try
                {
                    Directory.CreateDirectory(LogDirectory);
                }
                catch (Exception)
                {

                }
            }

            return LogDirectory + "\\" + TableName + ".time";
        }

        private void OutToTxt(string pText, string TableName)
        {
            File.WriteAllText(GetLogFilePath(TableName), pText);
            //FileStream fs = new FileStream(logFilePath, FileMode.Append);
            //StreamWriter sw = new StreamWriter(fs);
            //sw.WriteLine(pText);
            //sw.Close();
            //fs.Close();
        }

        /// <summary>
        /// 向文件里面写入同步记录
        /// </summary>
        /// <param name="dt"></param>
        public void RecordSyncLog(string pStr, string TableName)
        {
            OutToTxt(pStr, TableName);
        }

        
        /// <summary>
        /// 向文件里面读取同步记录
        /// </summary>
        /// <returns></returns>
        public List<string> ReadSyncLog(string TableName)
        {
            string[] str1 = { "" };
            try
            {
                str1 = File.ReadAllText(GetLogFilePath(TableName)).Split('|');
            }
            catch (Exception)
            {

            }

            return str1.ToList();
        }
        #endregion
    }
}
